Skip to content

FIX open streams filter by the auth data of the connect instead of the current one - #482

Merged
pubkey merged 3 commits into
masterfrom
claude/eloquent-hopper-6e78eb-stream-fresh-auth
Nov 30, 2026
Merged

pubkey merged 3 commits into
masterfrom
claude/eloquent-hopper-6e78eb-stream-fresh-auth

Conversation

@pubkey

@pubkey pubkey commented Nov 30, 2026

Copy link
Copy Markdown
Owner

Bug

The SSE routes /pullStream (replication endpoint) and /query/observe (REST endpoint) call the authHandler again before every event. But the result was only used to detect an expired or invalid auth:

  • In /pullStream the fresh auth data went into a local variable that was never read.
  • In /query/observe the reassigned authData was never used.

The documents were still filtered by the queryModifier with the auth data of the connect.

So when the auth data for the same headers changes while a stream is open, the stream keeps sending documents the user may no longer see. For example, the authHandler looks up the user's team in a database and the user is removed from the team. /pull and /query already use the current auth data on every request.

Fix

Both routes now build the document matcher (getDocAllowedMatcher) from the current auth data on every event and filter with it, before the server-only fields are removed.

For /query/observe, the RxQuery still runs with the query that was modified at connect time. Its results are additionally filtered by the current matcher. Documents the user lost access to are removed right away. Documents the user newly gained access to appear after a reconnect. Ending the stream whenever the modified query changes was not used, because a queryModifier that uses Date.now() would make it reconnect endlessly.

Tests

  • endpoint-replication.test.ts: should use the current auth data to filter the events of an open pullStream
  • endpoint-rest.test.ts: should use the current auth data to filter the results of an open stream

Both change the auth data of an open stream and fail without the fix.

Full unit suite: 109 passing. npm run check-types passes. An independent review confirmed the bug, the fix, and the order of matching before removing server-only fields. It ran the new tests 15 times with no failures.

Changelog: orga/changelog/fix-stream-fresh-auth.md


Generated by Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015P6w6gSU5X3tPquwWNPFbs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015P6w6gSU5X3tPquwWNPFbs
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

✅ Verify Test Reproduction: Tests FAILED without the fix (expected)

This confirms the changed tests correctly reproduce the bug that the source changes fix.

This workflow runs the changed tests without the source fix to verify they reproduce the bug.

Show output
=== test:unit output ===
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 test:unit
> npm run build && mocha --import=tsx ./test/unit.test.ts --bail --exit

npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 prebuild
> npm run prebuild:babel && npm run prebuild:scripts

npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 prebuild:babel
> rimraf -rf babel.config.js && cp node_modules/rxdb/babel.config.js babel.config.js

npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 prebuild:scripts
> rimraf -rf scripts/generate-plugins-package-json.mjs && cp node_modules/rxdb/scripts/generate-plugins-package-json.mjs scripts/generate-plugins-package-json.mjs


> rxdb-server@0.0.0 build
> npm run prebuild && rimraf -rf ./dist && npm run build:plugins && concurrently "npm run build:cjs" "npm run build:es" "npm run build:types" --success all

npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 prebuild
> npm run prebuild:babel && npm run prebuild:scripts

npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 prebuild:babel
> rimraf -rf babel.config.js && cp node_modules/rxdb/babel.config.js babel.config.js

npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 prebuild:scripts
> rimraf -rf scripts/generate-plugins-package-json.mjs && cp node_modules/rxdb/scripts/generate-plugins-package-json.mjs scripts/generate-plugins-package-json.mjs

npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 build:plugins
> node ./scripts/generate-plugins-package-json.mjs

./plugins/server
./plugins/adapter-express
./plugins/replication-server
./plugins/client-rest
[0] npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
[0] npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
[0] 
[0] > rxdb-server@0.0.0 build:cjs
[0] > rimraf -rf dist/cjs && cross-env NODE_ENV=es5 babel src --out-dir dist/cjs --source-maps --extensions ".ts,.js"
[0] 
[1] npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
[1] npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
[2] npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
[2] npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
[1] 
[1] > rxdb-server@0.0.0 build:es
[1] > rimraf -rf dist/esm && cross-env NODE_ENV=es6 babel src --out-dir dist/esm --source-maps --extensions ".ts,.js" && echo '{ "type": "module", "sideEffects": false }' > dist/esm/package.json
[1] 
[2] 
[2] > rxdb-server@0.0.0 build:types
[2] > rimraf -rf ./dist/types && tsc --project ./config/tsconfig.types.json
[2] 
[0] @babel/plugin-transform-template-literals: The 'loose' option has been deprecated, use the `ignoreToPrimitiveHint` and `mutableTemplateObject` assumptions instead (https://babeljs.io/assumptions).
[0] @babel/plugin-transform-classes: The 'loose' option has been deprecated, use the 'setClassMethods', 'constantSuper', 'superIsCallableConstructor', 'noClassCalls', 'noClassPrototypeAssign', and 'noConstructorPrimitiveReturn' assumptions instead (https://babeljs.io/assumptions).
[1] @babel/plugin-transform-template-literals: The 'loose' option has been deprecated, use the `ignoreToPrimitiveHint` and `mutableTemplateObject` assumptions instead (https://babeljs.io/assumptions).
[2] npm run build:types exited with code 0
[1] Successfully compiled 15 files with Babel (589ms).
[1] npm run build:es exited with code 0
[0] Successfully compiled 15 files with Babel (680ms).
[0] npm run build:cjs exited with code 0
DEFAULT_STORAGE: undefined
# use RxStorage: memory


  server.test.ts
    init
-------------- RxDB dev-mode warning -------------------------------
you are seeing this because you use the RxDB dev-mode plugin https://rxdb.info/dev-mode.html?console=dev-mode 
This is great in development mode, because it will run many checks to ensure
that you use RxDB correct. If you see this in production mode,
you did something wrong because the dev-mode plugin will decrease the performance.

🤗 Hint: To get the most out of RxDB, check out the Premium Plugins
to get access to faster storages and more professional features: https://rxdb.info/premium/?console=dev-mode 

💬 Need help? The RxDB Discord is the fastest place to reach maintainers: https://rxdb.info/chat/?console=dev-mode 

You can disable this warning by calling disableWarnings() from the dev-mode plugin.
---------------------------------------------------------------------
      �[32m✔�[39m init
    basics
      �[32m✔�[39m should start end stop the server (68ms)
      �[32m✔�[39m should apply CORS (45ms)
      �[32m✔�[39m should not combine Access-Control-Allow-Origin: * with Access-Control-Allow-Credentials: true
      �[32m✔�[39m should add multiple endpoints
      �[32m✔�[39m should end open streams on close()
      �[32m✔�[39m should resolve close() after the database was closed
      �[32m✔�[39m should resolve when close() is called multiple times
      �[32m✔�[39m should reject start() when the port is already in use
    express adapter
      �[32m✔�[39m should work on an existing express app that does not parse JSON bodies
    .doesContainRegexQuerySelector()
      �[32m✔�[39m should return false
      �[32m✔�[39m should return true
    .mergeServerDocumentFieldsMonad()
      �[32m✔�[39m should merge the documents
      �[32m✔�[39m should not add server-only fields when serverDoc is undefined (new document)
      �[32m✔�[39m should return falsy clientDoc as-is without transforming it
      �[32m✔�[39m should not add the field when serverDoc lacks it (new-doc-on-server)
      �[32m✔�[39m should strip client-sent values for server-only fields missing on serverDoc
      �[32m✔�[39m should not produce extra keys when serverDoc is missing the field (no false conflict)

  endpoint-rest.test.ts
    basics
      �[32m✔�[39m should start end stop the server
      �[32m✔�[39m should work without auth handler
    /query
      �[32m✔�[39m should not allow a query when validUntil of the auth data has expired
      �[32m✔�[39m should return the correct query results
      �[32m✔�[39m should respect the auth header and queryModifier
      �[32m✔�[39m should not allow $regex queries
      �[32m✔�[39m should have access-control-allow-credentials set to true
    /query/observe
      �[32m✔�[39m should return the correct query results (68ms)
      �[32m✔�[39m should emit the newest result last when the authHandler has a varying latency (435ms)
      �[32m✔�[39m should disconnect an open stream when the auth becomes invalid (3039ms)
      �[32m✔�[39m should should automatically reconnect (3036ms)
      �[32m✔�[39m should respect the auth header and queryModifier
      �[32m✔�[39m should not allow $regex queries
      �[32m✔�[39m should work when the base64-encoded query contains URL-reserved characters
      �[32m✔�[39m should observe queries that contain characters outside of Latin-1 (72ms)
      �[32m✔�[39m should keep the Latin-1 query encoding of older clients (52ms)
      �[32m✔�[39m should not keep the stream open when the client closes it while the authHandler runs (2060ms)
      1) should use the current auth data to filter the results of an open stream


  35 passing (9s)
  1 failing

  1) endpoint-rest.test.ts
       /query/observe
         should use the current auth data to filter the results of an open stream:

      AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected

+ [
+   {
+     age: 1,
+     firstName: 'alice',
+     lastName: '6Iw[kSv_BÜ',
+     passportId: 'alice-doc'
+   },
+   {
+     age: 1,
+     firstName: 'alice',
+     lastName: 'czDt}d]6',
+     passportId: 'alice-doc-2'
+   }
+ ]
- []

      + expected - actual

      -[
      -  {
      -    "age": 1
      -    "firstName": "alice"
      -    "lastName": "6Iw[kSv_BÜ"
      -    "passportId": "alice-doc"
      -  }
      -  {
      -    "age": 1
      -    "firstName": "alice"
      -    "lastName": "czDt}d]6"
      -    "passportId": "alice-doc-2"
      -  }
      -]
      +[]
      
      at Context.<anonymous> (test/unit/endpoint-rest.test.ts:681:20)





View full workflow run

pubkey commented Nov 30, 2026

Copy link
Copy Markdown
Owner Author

I pushed a follow-up commit. With this PR, the document matcher is built inside the stream for every event. When the endpoint's queryModifier throws for the current auth data (for example a user who is no longer in any team), that error ended up in an RxJS subscription without an error handler. RxJS then rethrows it asynchronously, which crashes the server process. Before this PR the same throw happened in the async route handler, where express catches it.

Both routes now end the stream when building the matcher throws, the same way they do when the authHandler throws. The new test should end the pullStream when the queryModifier throws for the current auth data crashed with Error: user is blocked before this commit. Full unit suite: 110 passing.


Generated by Claude Code

@pubkey
pubkey merged commit 1841e68 into master Nov 30, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants